package com.lsabcd.dubbo.test;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.*;

@Activate(group = {CommonConstants.CONSUMER})
public class TPMonitorFilter implements Filter {

    // ms
    private static final int interval = 60000;
    private static final int windowSize = 5000;
    // ms

    public TPMonitorFilter(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                LimiterManager.getInstance().statistics();
            }
        },5000,5000);
    }

    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String methodName = invocation.getMethodName();
        long start = System.currentTimeMillis();
        Result result = invoker.invoke(invocation);
        WindowItem item = new WindowItem(new Date(), (int) (System.currentTimeMillis() - start));
        LimiterManager.getInstance().collect(methodName,item);
        return result;
    }
}
